<template>
	<view class="forget-bg">
		<view class="forget-card">
			<view class="forget-input forget-margin-b">
				<input type="text" placeholder="备忘录标题" v-model="title" />
			</view>
			<view class="forget-input forget-margin-b">
				<textarea placeholder="备忘录内容" v-model="content" rows="6"></textarea>
			</view>
		</view>
		<view class="forget-btn">
			<button class="landing" type="primary" @click="editMemo">编辑保存</button>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				id: "",
				title: "",
				content: ""
			}
		},
		onLoad(option) {
			console.log('编辑ID: ' + option.id);
			this.getDataById(option.id);
		},
		methods: {
			getDataById(_id) {
				uniCloud.callFunction({
					name: "getMemoById",
					data: {
						"_id": _id
					},
					success: (e) => {
						console.log(e.result);
						this.title = e.result.data[0].title;
						this.content = e.result.data[0].content;
						this.id = e.result.data[0]._id;
					}
				})
			},
			editMemo() {
				console.log("--------------执行编辑操作--------------");
				uniCloud.callFunction({
					name: "editMemo",
					data: {
						"id": this.id,
						"title": this.title,
						"content": this.content
					},
					success: (e) => {
						uni.showToast({
							title: '编辑成功',
							duration: 2000
						});
						uni.navigateTo({
							url: "../list/list"
						})
					},
					fail: (err) => {
						console.error('编辑失败', err);
						uni.showToast({
							title: '编辑失败',
							icon: 'none',
							duration: 2000
						});
					}
				})
			}
		}
	}
</script>
<style>

</style>